﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using HZ.MBSM.DBUtility;
public partial class Deparment_Worker_Meeting_update : System.Web.UI.Page
{
    private string strTitle, strContent, strDate;
    private long strAccountid;
    private bool isMust;
    string strWithinInfoid;
    string IsReUpLoad, oldFilePath, oldFileTitle;

    protected void Page_Load(object sender, EventArgs e)
    {
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumDeparment);
        strWithinInfoid = SysFun.PasswordDecrypt(Request.QueryString["D_MeetingID"].Trim());
        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            strAccountid = _Account.AccountId;

            if (!IsPostBack)
            {
                ListInfor(strWithinInfoid);
            }
            else
            {
                IsReUpLoad = Request.Form["isReUpLoad"].Trim();   //0表示不更改附件 1 表示重新上传附件
                oldFilePath = Request.Form["OldFilePath"].Trim();   //旧附件文件存储地址
                oldFileTitle = Request.Form["OldFileTitle"].Trim();   //旧附件文件标题  

                isMust = checkValue(Request);
                Save();
            }

        }
    }

    /// <summary>
    /// 显示信息
    /// </summary>
    /// <param name="strWithinInfoid"></param>
    private void ListInfor(string strWithinInfoid)
    {
        string sql = "select D_MeetingID,convert(varchar(100),AddDate,23) as AddDate ,Title,uploadFile,uploadFilePath, Content from D_Meeting where D_MeetingID=" + strWithinInfoid;
        Db db = new Db();
        DataTable dt = db.DataTable(sql);
        if (dt.Rows.Count > 0)
        {
            txtName.Text = dt.Rows[0]["title"].ToString();
            txtContent.Text = dt.Rows[0]["content"].ToString();
            string htmlContent = "";
            if (dt.Rows[0]["uploadfile"].ToString().Trim() != "")
            {
                htmlContent = "<a target='_blank' href='" + dt.Rows[0]["uploadfilepath"].ToString() + "'><b>" + dt.Rows[0]["uploadfile"].ToString() + "</b></a>" +
                             "<input type='hidden' name='OldFilePath' id='OldFilePath' value='" + dt.Rows[0]["uploadfilepath"].ToString() + "'/>" +   //旧附件地址
                             "<input type='hidden' name='OldFileTitle' id='OldFileTitle' value='" + dt.Rows[0]["uploadfile"].ToString() + "'/>" +     //就附件标题
                             "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" +
                            "<input type='button' name='ReUpLoad' id='ReUpLoad' value='重新上传' class='button_bg' style=\"width='60px'\" onclick=\"javascript:listFileUpLoad('fj')\"/>" +
                            "<input type='hidden' name='isReUpLoad' id='isReUpLoad' value='0'>";
            }
            else
            {
                htmlContent = "<b>无附件</b>" +
                             "<input type='hidden' name='OldFilePath' id='OldFilePath' value='" + dt.Rows[0]["uploadfilepath"].ToString() + "'/>" +   //旧附件地址
                             "<input type='hidden' name='OldFileTitle' id='OldFileTitle' value='" + dt.Rows[0]["uploadfile"].ToString() + "'/>" +     //就附件标题
                             "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" +
                            "<input type='button' name='ReUpLoad' id='ReUpLoad' value='重新上传' class='button_bg' style=\"width='60px'\" onclick=\"javascript:listFileUpLoad('fj')\"/>" +
                            "<input type='hidden' name='isReUpLoad' id='isReUpLoad' value='0'>";
            }
            txtfj.Text = htmlContent;

        }
    }

    /// <summary>
    /// 保存信息
    /// </summary>
    private void Save()
    {
        if (isMust)
        {
            string uploadFile = "";  //附件文件名标题
            string uploadFilePath = "";   //附件存储路径
            string downLoadUrl = "";   //下载地址

            if (IsReUpLoad == "1" && FileUpload1.HasFile)
            {
                //删除旧附件
                string oldFactPath = Server.MapPath(oldFilePath);

                System.IO.File.Delete(oldFactPath);




                //保存新附件
                uploadFile = FileUpload1.FileName;

                DateTime dtime = DateTime.Now;
                string filename = dtime.ToShortDateString().Replace("-", "") +
                     "-" + dtime.Hour.ToString() + dtime.Minute.ToString() + dtime.Second.ToString() +
                     "-" + new Random().Next(10000).ToString() + new System.IO.FileInfo(uploadFile).Extension;
                downLoadUrl = "../DupdateFile/" + filename;

                uploadFilePath = System.Web.HttpContext.Current.Request.MapPath("../DupdateFile/") + filename;
                FileUpload1.SaveAs(uploadFilePath);
            }
            else
            {
                downLoadUrl = oldFilePath;    //保留旧附件文件存储地址
                uploadFile = oldFileTitle;    //保留旧附件文件存储地址
            }

            int succ = 0;
            string procName = "";

            procName = "P_DMeeting_update";

            SqlParameter[] para = new SqlParameter[8];
            para[0] = new SqlParameter("@MeetingID", strWithinInfoid);
            para[1] = new SqlParameter("@title", strTitle);
            para[2] = new SqlParameter("@content", strContent);
            para[3] = new SqlParameter("@adddate", strDate);
            para[4] = new SqlParameter("@accountid", strAccountid);
            para[5] = new SqlParameter("@uploadfile", uploadFile);
            para[6] = new SqlParameter("@uploadfilepath", downLoadUrl);
            para[7] = new SqlParameter("@v_Success", succ);

            para[7].Direction = ParameterDirection.Output;
            Db db = new Db();
            db.ExecuteProcedure(procName, para);
            db.Close();
            succ = (int)para[7].Value;
            if (succ == 1)
            {
                Response.Write("<script>alert('信息修改成功!');self.close();</script>");
            }
            else
            {
                Response.Write("<script>alert('信息修改失败!')</script>");
            }
        }
        else
        {
            Response.Write("<script>alert('请将信息填写完整!!!')</script>");
        }
    }

    /// <summary>
    /// 验证必填项是否有空
    /// </summary>
    /// <returns></returns>
    private bool checkValue(HttpRequest request)
    {
        strTitle = request.Form["txtName"].Trim();
        strContent = txtContent.Text;
        strDate = txtAddDate.Text.Trim();
        if (strTitle == "" || strContent == "" || strDate=="")
        {
            return false;
        }
        else
        {
            return true;
        }
    }
}
